<template>
  <div class="shop-detail">
    <div class="back" slot="left" @click="backClick">
      <img src="~assets/img/common/back.svg" alt="">
    </div>
    <shop-swiper :banners="banners"/>
    <div class="goods-price">
      ￥{{message.goods_price}}
    </div>
    <div class="goods-name">
      <div class="name">
        {{message.goods_name}}
      </div>
      <div class="collect">
        <div class="collect-img" 
        :class="{active:isActive}"
        @click="changeBg"></div>
        <p>收藏</p>
      </div>
    </div>
    <div class="goods-content">
      <h3 class="title">图文详情</h3>
      <div v-html="message.goods_introduce"></div>
    </div>
    <shop-bar @addCart="addShopCart"/>
  </div>
</template>

<script>
import ShopSwiper from './ShopSwiper'
import ShopBar from './ShopBar'
import {getShopDetailData} from 'network/shop'
import { mapActions } from 'vuex'
export default {
  name:'ShopDetail',
  components:{
    ShopSwiper,
    ShopBar
  },
  data(){
    return{
      goods_id:null,
      banners:[],
      message:{},
      isActive:false
    }
  },
  activated(){
    this.getShopDetailData()
  },
  deactivated(){
    this.goods_id=''
  },
  methods:{
    ...mapActions(['addCart']),
    getShopDetailData(){
      this.goods_id = this.$route.params.goods_id
      getShopDetailData(this.goods_id).then(res => {
      this.message = res.data.message
      
      this.banners = res.data.message.pics 
    })
    },
    backClick(){
      this.$router.back()
    },
    addShopCart(){
      // 1.获取购物车需要展示的信息
      const product = {}
      // 没有图片的会报错，自己想的方法解决不了
      product.image = this.banners[0].pics_sma 
      product.title = this.message.goods_name
      product.price = this.message.goods_price
      product.goods_id = this.goods_id

      // 2.将商品添加到购物车(promise、mapActins)
      // this.$store.dispatch('addCart',product).then(res =>{
      //   console.log(res)
      // })
      this.addCart(product).then(res =>{ 
        this.$toast.show(res,2000)
      })
      
    },
    changeBg(){
      this.isActive = !this.isActive;
    }
  }
}
</script>

<style scoped>
  .shop-detail{
    margin-bottom: 49px;
  }
  .back{
    width: 40px;
    height: 40px;
    text-align: center;
    position:fixed;
    top: 10px;
    left: 10px;
    background:rgba(10, 10, 10, 0.1);
    border-radius: 50%;
    z-index: 999;
  }
  .back img{
    margin-top: 8px;
  }

  .goods-price{
    color: var(--color-tint);
    font-size: 18px;
    font-weight: bold;
    margin-left: 10px;
  }
  .goods-name{
    display: flex;
    font-size: 14px;
  }
  .name{
    flex: 3;
    padding: 4px 10px;
    line-height: 1.4;
    border-right: 2px solid #ccc;
  }
  .collect{
    flex: 1;
    text-align: center;
  }
  .collect .collect-img{
    width: 28px;
    height: 28px;
    margin: 0 auto;
    background: url('~assets/img/detail/detail_bottom.png');
    background-repeat: no-repeat;
    background-size: 100%;
    background-position: 0 0px;
}
  .collect .collect-img.active{
    background-position: 0 -33px;
  }
  .goods-content{
    margin: 0 10px;
  }
  .goods-content .title{
    line-height: 40px;
    font-size: 20px;
    margin: 5px 0;
    padding-left: 10px;
    color:var(--color-tint);
    border-left: 3px solid var(--color-tint);
  }

</style>